package com.jhws.sys.cms.api;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.jhws.business.customer.bo.CustomContentInfo;
import com.jhws.common.base.bo.JsonRespData;
import com.jhws.common.base.bo.Global;
import com.jhws.sys.cms.service.ResourceService;
import com.jhws.util.StringUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/3/31.
 */
@Controller
@RequestMapping("/v2/cms")
public class CmsApiV2 extends CmsApi {
    @Resource
    private ResourceService rs;

    /**
     * 根据小区获取资源
     *
     * @param typeCode 资源code
     * @param commCode 小区编号
     * @return
     * @throws IOException
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "anon/queryResourcesByComm")
    @ResponseBody
    public JsonRespData queryResourcesByComm(String typeCode, String commCode) throws IOException {
        if (StringUtils.isEmpty(typeCode) || StringUtils.isEmpty(commCode)) {
            return new JsonRespData("queryResourcesByComm", -1, "参数不能为空！");
        }
        Map param = Maps.newHashMap();
        param.put("type_code", typeCode);
        param.put("commCode", commCode);
        param.put("isApproved", Global.CMS_APPROVED);
        List<Map<String, Object>> result = rs.queryResourceList(true, typeCode, null, commCode);
        for (Map<String, Object> o : result) {
            String customContent = MapUtils.getString(o, "customContent");
            if (!org.apache.commons.lang.StringUtils.isEmpty(customContent)) {
                List<CustomContentInfo> customContentInfos = JSON.parseArray(customContent, CustomContentInfo.class);
                o.put("customContent", customContentInfos);
            }
        }
        return new JsonRespData("queryResourcesByComm", 1, result);
    }

    /**
     * 获取主页内容
     * @param commCode
     * @return
     * @throws IOException
     */
    @Override
    @RequestMapping("anon/getHomepageContent")
    public JsonRespData getHomepageContent(String commCode) throws IOException {
        return super.getHomepageContent(commCode);
    }

    /**
     * 获取门口机广告
     * @param commCode   小区编号
     * @param date       日期
     * @param screenType
     * @return
     * @throws IOException
     */
    @Override
    @RequestMapping("anon/getOutdoorAdvertising")
    public JsonRespData getOutdoorAdvertising(String commCode, String date, String screenType) throws IOException {
        return super.getOutdoorAdvertising(commCode, date, screenType);
    }

    /**
     * 获取门口机视频广告
     * @param commCode 小区编号
     * @param date     日期
     * @return
     * @throws IOException
     */
    @Override
    @RequestMapping("anon/getOutdoorVideoAds")
    public JsonRespData getOutdoorVideoAds(String commCode, String date) throws IOException {
        return super.getOutdoorVideoAds(commCode, date);
    }
}
